package com.easyaccount.service;

import com.easyaccount.bean.MenuTree;
import com.easyaccount.bean.MenuVO;
import com.easyaccount.commons.CommonResp;
import com.easyaccount.commons.TablePage;
import com.easyaccount.dao.MenuMapper;
import com.easyaccount.dao.RoleMenuMapper;
import com.easyaccount.dao.UserRoleMapper;
import com.easyaccount.exception.BizException;
import com.easyaccount.model.Menu;
import com.easyaccount.model.RoleMenu;
import com.easyaccount.model.UserRole;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class MenuService {
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Resource
    private MenuMapper menuMapper;

    public CommonResp save(Menu menu){
        int count1 = menuMapper.selectByNameCount(menu.getMenuName());
        if(count1 > 0) throw new BizException("菜单名称已存在",444);

        int i = menuMapper.insertSelective(menu);
        if (i == 0) throw new BizException("添加失败",444);

        return CommonResp.success();
    }

    public CommonResp update(Menu menu){
        int count1 = menuMapper.selectByIdAndNameCount(menu);
        if(count1 > 0) throw new BizException("菜单名称已存在",444);

        int i = menuMapper.updateByPrimaryKeySelective(menu);
        if (i == 0) throw new BizException("添加失败",444);

        return CommonResp.success();
    }

    public CommonResp delete(Menu menu){
        Menu result = menuMapper.selectByPrimaryKey(menu.getMenuId());
        if(result == null) throw new BizException("菜单不存在",444);

        List<Menu> menus = menuMapper.selectByParentId(menu.getParentId());
        if (menus != null && menus.size() > 0) throw new BizException("该菜单下面存在子菜单，无法删除",444);

        int i = menuMapper.deleteByPrimaryKey(menu.getMenuId());
        if (i == 0) throw new BizException("删除失败",444);

        return CommonResp.success();
    }

    public TablePage searchByName(TablePage tablePage, String menuName){
        int size = tablePage.getLimit();
        int start = (tablePage.getPage() - 1) * size;

        List<MenuVO> menuVOS = new ArrayList<>();
        List<Menu> menus = menuMapper.selectByName(start, size, menuName);
        for (Menu menu : menus) {
            MenuVO menuVO = new MenuVO();
            BeanUtils.copyProperties(menu,menuVO);
            Menu p = menuMapper.selectByPrimaryKey(menu.getParentId());
            if (p != null) menuVO.setParentName(p.getMenuName());
            menuVOS.add(menuVO);
        }

        int count = menuMapper.selectByNameCount(menuName);
        tablePage.setCount(count);
        tablePage.setData(menuVOS);
        return tablePage;
    }

    public CommonResp list(){
        List<Menu> menus = menuMapper.selectAll();
        return CommonResp.success(menus);
    }

    public List<Menu> getMenuAll(){
        return menuMapper.selectAll();
    }

    public List<MenuTree> toTree(List<Menu> menus){
//        List<Menu> menus = getMenu(userId);

        List<MenuTree> trees = new ArrayList<>();
        for (Menu menu : menus) {
            Integer parentId = menu.getParentId();
            if(parentId == 0){
                MenuTree menuTree = new MenuTree();
                menuTree.setChildrens(new ArrayList<>());
                BeanUtils.copyProperties(menu,menuTree);
                trees.add(menuTree);
            }
        }

        if(trees.size() > 1) {
            // 排序
            trees.sort(new Comparator<MenuTree>() {
                @Override
                public int compare(MenuTree o1, MenuTree o2) {
                    return o1.getOrderNum() - o2.getOrderNum();
                }
            });
        }
        create(trees,menus);

        return trees;
    }

    public List<Menu> getMenu(Integer userId){
        UserRole userRole = userRoleMapper.selectByUserId(userId);    // 通过userid获取roleid
        List<RoleMenu> roleMenus = roleMenuMapper.selectByRoleId(userRole.getRoleId()); // 通过roleid获取角色对应的menuid
        List<Integer> menuIds = new ArrayList<>();
        roleMenus.forEach(roleMenu -> {
            menuIds.add(roleMenu.getMenuId()); // 将menuid添加到list
        });
        List<Menu> menus = menuMapper.selectByPrimaryKeys(menuIds);  // 通过menuid获取menu
        return menus;
    }

    private void create(List<MenuTree> root,List<Menu> menus){
        for (MenuTree menuTree : root) {
            Integer menuId = menuTree.getMenuId();
            List<MenuTree> childrens = menuTree.getChildrens();

            for (Menu menu : menus) {
                Integer parentId = menu.getParentId();
                if(menuId == parentId) {
                    MenuTree mt = new MenuTree();
                    mt.setChildrens(new ArrayList<>());
                    BeanUtils.copyProperties(menu, mt);
                    childrens.add(mt);
                }
            }

            // 排序
            if (childrens != null && childrens.size() > 1) {
                childrens.sort(new Comparator<MenuTree>() {
                    @Override
                    public int compare(MenuTree o1, MenuTree o2) {
                        return o1.getOrderNum() - o2.getOrderNum();
                    }
                });

                create(childrens,menus);
            }

        }
    }

}
